/**
 * 
 */
package com.magnusart.scrumify.dao;

import static org.junit.Assert.fail;

import java.util.List;

import org.junit.After;
import org.junit.Before;

import com.magnusart.scrumify.dao.CommonDAO;
import com.magnusart.scrumppm.model.Project;
import com.magnusart.scrumppm.model.ProjectBuilder;
import com.magnusart.scrumppm.model.Story;
import com.magnusart.scrumppm.model.StoryBuilder;

/**
 * @author Magnus Andersson
 *
 */
public class StoryDAOSpecification_HsqlDb extends GenericDAOSpecification<Project, Story, StoryBuilder, CommonDAO<Project, Story, StoryBuilder> >
{
	private DBTestUtil.SeamedHsqlDbDAOFactory factory;
	private CommonDAO<Project, Project, ProjectBuilder> projectDao;
	
	@Before
	public void setUp() throws Exception {

		DBTestUtil.setUpTempDatabase();
		
		factory = new DBTestUtil.SeamedHsqlDbDAOFactory();

		// Use our seamed instance with a special sql connection.
		dao = factory.getStoryDAO();
		projectDao = factory.getProjectDAO();
		List<Project> projs = projectDao.getAll( null );
		
		if( projs.size() > 0 )
			parent = projs.get( 0 );
		else
			fail( "No open parent project, needed to be able to test stories" );

		builder = new StoryBuilder();
		builder.setTitle( "Test title" );
		builder.setDescription( "description" );
		builder.setDeliverable( "deliverable" );
		
		entry1 = new Story( builder );
		entry2 = new Story( builder );
		
		builder = new StoryBuilder();
		builder.setTitle( "Entry3" );
		builder.setDescription( "Entry3 Desc");
		builder.setDeliverable( "Entry3 Deliverable" );
		
		entry3 = new Story( builder );
		
		builder.setTitle( "Entry3 Updated" );
		builder.setDescription( "Entry3 Desc Updated" );
	}
	

	@After
	public void tearDown() throws Exception {
		DBTestUtil.closeConn();
	}


	@Override
	boolean deepEquality( Story a, StoryBuilder b) {
			Story c = new Story( b );
			return compareWithoutID( a, c ) && a.getID() == c.getID();
	}


	@Override
	boolean compareWithoutID( Story a, Story c ) {
		return a.getTitle().equalsIgnoreCase( c.getTitle() ) &&
		   a.getDescription().equals( c.getDescription()) &&
		   a.getDeliverable().equals( c.getDeliverable() ) &&
		   a.getPriority() == c.getPriority() &&
		   a.getEstimatedUnits().equals( c.getEstimatedUnits() ) &&
		   a.isDone() == c.isDone() && a.skip() == c.skip() &&
		   a.hasTasks() == c.hasTasks();
	}
}
